<?php
/**
 * Created by PhpStorm.
 * User: h
 * Date: 2021/4/26
 * Time: 10:53
 */

namespace app\api\controller\v1;

use app\common\model\Article as ArticleModel;
use app\common\model\CaseModel;
use app\common\model\Friend;
use app\common\model\Cooperate;
use app\common\model\Cate;

class Cases extends BaseController
{
    public function index()
    {
        $data = [
            'friend' => $this->friend(),
            'cooperate' => $this->cooperate(),
            'case' => $this->caseindex()
        ];
        return sucessMsg(200, '获取成功！', $data);
    }

    //实例展示
    public function caseindex()
    {
        $datas = CaseModel::where('status', 1)
            ->field('id,title,image,sort')->order('sort asc')->limit(0, 7)->select();
        foreach ($datas as &$v) {
            if (!empty($v['image']))
                $v['image'] = env('app.yuming') . $v['image'];
        }
        return $datas;
    }

    //合作伙伴
    public function friend()
    {
        $datas = Friend::where(['status' => 1])->order('sort asc')->limit(0, 10)->select();
        foreach ($datas as &$v) {
            if (!empty($v['image']))
                $v['image'] = env('app.yuming') . $v['image'];
        }
        return $datas;
    }

    //合作项目
    public function cooperate()
    {
        $datas = Cooperate::where(['status' => 1])->order('sort asc')->select();
        foreach ($datas as &$v) {
            if (!empty($v['image']))
                $v['image'] = env('app.yuming') . $v['image'];
        }
        return $datas;
    }

    public function caselist()
    {
        $page = input('page/d', 1);
        $cate = Cate::where(['type' => 1, 'status' => 1])->field('id,name,pid')->select()->toArray();
        //  取出所有分类的id
        $cate_id = array_column($cate, 'id');
        $cate = getTree($cate);
        $id = input('cate_id/d');
        //$where = ['cate_id' => $id];
        if (!empty($id)) {
            $where = ['cate_id' => $id];
            $cate_this = Cate::find($id);
        } else {
            $where[] = ['cate_id', 'in', $cate_id];
            $cate_this = ['id' => 0, 'name' => '实例展示'];
        }
        if (empty($cate_id)) {
            $cate_id = Cate::where(['type' => 1, 'status' => 1])->column('id');
            $data = CaseModel::where(['status' => 1])
                ->where('cate_id', 'in', $cate_id)
                ->field('id,image,title,file_path')
                ->order('sort asc')
                ->limit(($page - 1) * 15, 15)
                ->select()
                ->toArray();
            //$cate_this = ['id' => 0, 'name' => '实例展示'];
            $row = CaseModel::where(['status' => 1])
                ->where('cate_id', 'in', $cate_id)
                ->field('id')
                ->count();
        } else {
            //$cate_this = Cate::field('id,name')->find($cate_id);
            $data = CaseModel::where(['status' => 1, 'cate_id' => $cate_id])
                ->field('id,image,title,file_path')
                ->order('sort asc')
                ->limit(($page - 1) * 15, 15)
                ->select()->toArray();
        }
        //分类下的数据
        $res = CaseModel::where($where)
            ->where('status', 1)
            ->field('id,image,title,file_path,created_at,content,param')
            ->order('sort asc')
            ->limit(($page - 1) * 9, 9)
            ->select()
            ->toArray();
        if ($res) {
            $res = changeImage($res, 'image');
            $res = changeTime($res, 'created_at');
            $res = delHtml($res, 'content');
        }
        $maxpage = CaseModel::where($where)
            ->where('status', 1)
            ->field('id')
            ->count();
        $maxpage = ceil($maxpage / 9);
        if ($data) {
            foreach ($data as &$v) {
                if (!empty($v['image']))
                    $v['image'] = env('app.yuming') . $v['image'];
                if (!empty($v['file_path']))
                    $v['file_path'] = env('app.yuming') . $v['file_path'];
            }
        }
        //全部数据
        $all = CaseModel::where('status', 1)
            ->where($where)
            ->field('id,image,title,file_path,param,content')
            ->order('sort asc')
            ->limit(($page - 1) * 9, 9)
            ->select()
            ->toArray();
        if ($all) {
            foreach ($all as &$v) {
                if (!empty($v['image']))
                    $v['image'] = env('app.yuming') . $v['image'];
                if (!empty($v['file_path']))
                    $v['file_path'] = env('app.yuming') . $v['file_path'];
            }
        }
        return sucessMsg(200, '获取成功', [
            'cate_this' => $cate_this,
            'data' => $data,
            'maxpage' => $maxpage,
            'cate' => $cate,
            'res' => $res,
            'all'=>$all
        ]);
    }

    public function content()
    {
        $id = input('id');
        if (!$id) {
            return errMsg(400, '请传入参数');
        }
        $data = CaseModel::find($id);
        $data->image = env('app.yuming') . $data->image;
        $data->param = json_decode($data->param);
        $pdf = $data->file_path;
        if (is_file($pdf)) {
            $data->file_size = trans_byte(filesize($pdf));
            $finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型
            $lei = finfo_file($finfo, $pdf);
            finfo_close($finfo);
            $data->file_type = getSub($lei, '/', ';');
        } else {
            $data->file_size = '0kb';
            $data->file_type = "暂无文件";
        }
        $data->file_path = env('app.yuming') . $data->file_path;

        $about_sucai = CaseModel::where(['cate_id' => $data->cate_id, 'status' => 1])
            ->where('id', '<>', $id)
            ->field('id,image,title')
            ->limit(0, 4)
            ->order('sort asc')
            ->select()->toArray();
        if ($about_sucai) {
            foreach ($about_sucai as &$v) {
                if (!empty($v['image']))
                    $v['image'] = env('app.yuming') . $v['image'];
            }
        }
        $sucai_id = array_column($about_sucai, 'id');
        $sucai_id = array_merge($sucai_id, [intval($id)]);
        $cate = Cate::where(['status' => 1, 'type' => 1, 'pid' => 0])->field('id,name')->select()->toArray();
        $cate = array_column($cate, 'id');
        $about_tuijian = CaseModel::where(['status' => 1])
            ->where('cate_id', 'in', $cate)
            ->where('id', 'not in', $sucai_id)
            ->field('id,image,title')
            ->limit(0, 8)
            ->order('sort asc')
            ->select();
        if ($about_tuijian) {
            foreach ($about_tuijian as &$v) {
                $v['image'] = env('app.yuming') . $v['image'];
            }
        }
        return sucessMsg(200, '获取成功', ['data' => $data, 'about_shili' => $about_sucai, 'about_tuijian' => $about_tuijian]);
    }
}